package thread;

public class Demo19 {
    public static void main(String[] args) {
        Object locker1=new Object();
        Object locker2=new Object();
        Object locker3=new Object();
        Thread a=new Thread(()->{
            synchronized (locker1){
                try {
                    locker1.wait();
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"a");
        Thread b=new Thread(()->{
            synchronized (locker2){
                try {
                    locker2.wait();
                    System.out.print(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            synchronized (locker1){
                locker1.notify();
            }
        },"b");
        Thread c=new Thread(()->{
            synchronized (locker3){
                System.out.print(Thread.currentThread().getName());
            }
            synchronized (locker2){
                locker2.notify();
            }
        },"c");
        a.start();
        b.start();
        c.start();
    }
    public static void main2(String[] args) throws InterruptedException {
        Object locker1=new Object();
        Object locker2=new Object();
        Thread t1=new Thread(()->{
            synchronized (locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2){
                    System.out.println("t1获取到两把锁");
                }
            }
        });
        Thread t2=new Thread(()->{
            synchronized (locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2){
                    System.out.println("t2获取到两把锁");
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
    }
    public static void main1(String[] args) throws InterruptedException {
        Object locker1=new Object();
        Object locker2=new Object();
        Thread t1=new Thread(()->{
            synchronized (locker1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker2){
                    System.out.println("t1获取到两把锁");
                }
            }
        });
        Thread t2=new Thread(()->{
            synchronized (locker2){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (locker1){
                    System.out.println("t2获取到两把锁");
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        /*
        1.构成死锁的场景
        a:一个线程一把锁连续加锁=>可重入锁
        b:两个线程两把锁=>代码如何编写
        c:N个线程M把锁=>哲学家就餐问题
        2.死锁的四个必要条件
        a:互斥
        b:不可剥夺
        c:请求和保持(保持当前锁 请求下个锁)
        d:循环等待
        3.如何避免死锁
        打破c:把嵌套锁改成并列锁
        打破d:加锁的顺序做出约定
        * */
    }
}
